/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestionnaires;

import utilisateurs.modeles.Achat;
import utilisateurs.modeles.Utilisateur;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class GestionnaireAchat extends AbstractFacade<Achat> {

    @PersistenceContext(unitName = "BlueRayPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public GestionnaireAchat() {
        super(Achat.class);
    }

    public Achat create() {
        Achat checkout = new Achat();
        create(checkout);
        return checkout;
    }

    public Achat getLast(Utilisateur u) {
        Query query = em.createQuery("SELECT c FROM Utilisateur u, IN(u.commandes) c WHERE u.id = :id AND c.valid = FALSE");
        query.setParameter("id", u.getId());

        try {
            return (Achat) query.getSingleResult();
        } catch(NoResultException e) {
            return null;
        }
    }
}

